var xtgg = {
    setBtnStatus: function () {
        var row = $('#grid_xtgg').datagrid('getSelected');
        if (row) {
            if (row.ggzt == '1') {
                $("#btn_xtgg_unpub").show();
                $("#btn_xtgg_pub,#btn_xtgg_mod,#btn_xtgg_del").hide();
                if(row.zd == '是'){
                    $("#btn_xtgg_top").hide();
                    $("#btn_xtgg_untop").show();
                }else{
                    $("#btn_xtgg_top").show();
                    $("#btn_xtgg_untop").hide();
                }
            } else {
                $("#btn_xtgg_unpub").hide();
                $("#btn_xtgg_pub,#btn_xtgg_mod,#btn_xtgg_del").show();
                $("#btn_xtgg_top").hide();
                $("#btn_xtgg_untop").hide();
            }

        } else {
            $("#btn_xtgg_mod,#btn_xtgg_del,#btn_xtgg_pub,#btn_xtgg_unpub").hide();
        }
    },
    showWindow: function (xtggdata) {
        var tit = xtggdata["xtgg.id"] ? "修改系统公告" : "添加系统公告";
        xtgg.win = $.window({
            title: tit,
            iconCls: 'icon-add',
            width: 800,
            height: 500,
            cache: false,
            url: $.webPath() + "/base/xtgg/xtgggl_content",
            data: xtggdata,
            buttons: [
                {
                    text: '保存',
                    handler: function () {
                        $.messager.progress();
                        $("#form_xtggcontent").form('submit', {
                            url: $.webPath() + "/base/xtgg/xtgg_save",
                            onSubmit: function () {
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    $.messager.progress('close');
                                }
                                return isValid;
                            },
                            success: function (data) {
                                $.messager.progress('close');
                                var json = $.parseJSON(data);
                                if (json.statusCode == 200) {
                                    $(xtgg.win).window("close");
                                    xtgg_search();
                                    $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                                } else
                                    $.messager.alert('提示', json.message, 'info');
                            }
                        });
                    }
                }
            ]
        });
    },
    showReadOnlyWindow: function (xtggdata) {
        xtggdata.xtggReadonly = true;
        xtgg.win = $.window({
            title: "公告详细",
            iconCls: 'icon-add',
            width: 800,
            height: 500,
            cache: false,
            url: $.webPath() + "/base/xtgg/xtgggl_content",
            data: xtggdata,
            buttons: []
        });
    },
    initDataGrid: function () {
        $('#grid_xtgg').datagrid({
            url: $.webPath() + "/base/xtgg/xtgg_list",
            toolbar: "#toolbar_xtgggl",
            pagination: true,
            rownumbers: true,
            pageNumber: 1,
            fitColumns: true,
            singleSelect: true,
            fit: true,
            striped: true,
            onLoadSuccess: xtgg.setBtnStatus,
            /* onLoadSuccess:function () {
             $('#grid_xtgg').datagrid('getSelected');
             var row = $('#grid_xtgg').datagrid('getSelected');
             if (row.gsjgList_id != null){
             $('#grid_xtgg').datagrid('getSelected').find('#gg_gsjg_tree').tree('setValues', row.gsjgList_id.split(","));
             }
             },*/
            onClickRow: xtgg.setBtnStatus,
            onDblClickCell: function (index, field, value) {
                if (field == "ggbt") {
                    var row = $(this).datagrid("getSelected");
                    if (row.ggzt == "1") {
                        xtgg.showReadOnlyWindow({"xtgg.id": row.id});
                    } else {
                        xtgg.showWindow({"xtgg.id": row.id});
                    }
                }
            },
            columns: [
                [
                    {field: 'ggbt', title: '公告标题', width: 105},
                    {field: 'ggnr', title: '公告内容', width: 335},
                    {field: 'gsjgList', title: '发布公司', width: 313},
                    {field: 'ggzt', title: '公告状态', width: 65,
                        formatter: function (value) {
                            if (value == '0')
                                return '未发布';
                            if (value == '1')
                                return '<span style="color: #ff0000">已发布<span>';
                            if (value == '2')
                                return '已撤销';
                        }
                    },
                    {field: 'zd', title: '置顶', width: 50},
                    {field: 'createUser_xm', title: '创建人', width: 50 },
                    {field: 'dateCreated_time', title: '创建时间', width: 125 },
                    {field: 'fbdate_time', title: '发布时间', width: 126}
                ]
            ]

        });
    },
    initBtn: function () {
        $("#btn_xtgg_add").click(function () {
            xtgg.showWindow({});
        });
        $("#btn_xtgg_mod").click(function () {
            var row = $('#grid_xtgg').datagrid("getSelected");
            xtgg.showWindow({"xtgg.id": row.id});
        });
        $("#btn_xtgg_del").click(function () {
            var row = $('#grid_xtgg').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否删除系统公告', function (r) {
                    if (r) {
                        var index = $('#grid_xtgg').datagrid('getRowIndex', row);
                        $.post($.webPath() + '/base/xtgg/xtgg_del', {"xtgg.id": row.id}, function (data) {
                            var json = $.parseJSON(data);
                            if (json.statusCode == '200') {
                                // $('#gg_gsjg_tree').tree('clear');
                                $('#grid_xtgg').datagrid('deleteRow', index);
                                xtgg.setBtnStatus();
                                $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                            }
                            else
                                $.messager.alert('提示', json.message, 'info');
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要删除的系统公告", 'info');
            }
        });
        $("#btn_xtgg_top").click(function () {
            var row = $('#grid_xtgg').datagrid('getSelected');
            $.post($.webPath() + "/base/xtgg/xtgg_top", {"xtgg.id": row.id}, function (data) {
                var json = $.parseJSON(data);
                if (json.statusCode == '200') {
                    xtgg_search();
                    $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                }
                else
                    $.messager.alert('提示', json.message, 'info');
            })
        })
        $("#btn_xtgg_untop").click(function () {
            var row = $('#grid_xtgg').datagrid('getSelected');
            $.post($.webPath() + "/base/xtgg/xtgg_untop", {"xtgg.id": row.id}, function (data) {
                var json = $.parseJSON(data);
                if (json.statusCode == '200') {
                    xtgg_search();
                    $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                }
                else
                    $.messager.alert('提示', json.message, 'info');
            })
        })
        //发布之前选择公司 部门
        $("#btn_xtgg_pub").click(function (title, href) {
            var href = $.webPath() + "/base/xtgg/xzgs";
            xzgs_showDialog(title, href);
        });
        var xtgg_win;

        function xzgs_showDialog(title, href) {
            var button = [
                {text: '确认', handler: function () {
                    var nodes = $('#gg_gsjg_tree').tree('getChecked');
                    var ids = '';
                    for (var i = 0; i < nodes.length; i++) {
                        ids += nodes[i].id + ',';
                    }
                    if (ids)
                        ids = ids.substring(0, ids.length - 1);
                    href = $.webPath() + "/base/xtgg/xtgg_pub";
                    $(xtgg_win).window('close');
                    sendXtgg(href, row.id, ids);
                }}
            ];
            var row = $('#grid_xtgg').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否发布系统公告', function (r) {
                    if (r) {
                        xtgg_win = $.window({
                            title: '选择发布部门',
                            iconCls: 'icon-add2',
                            url: href,
                            width: 280,
                            height: 410,
                            buttons: button
                        });
                    }
                })
            } else {
                $.messager.alert('提示', "请选择要发布的系统公告", 'info');
            }
        }

        function sendXtgg(href, rowid, ids) {
            $.post(href, {"xtgg.id": rowid, 'gsjgId': ids}, function (data) {
                var json = $.parseJSON(data);
                if (json.statusCode == '200') {
                    xtgg_search();
                    $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                }
                else
                    $.messager.alert('提示', json.message, 'info');
            });
        }

        $("#btn_xtgg_unpub").click(function () {
            var row = $('#grid_xtgg').datagrid('getSelected');
            if (row) {
                $.messager.confirm('确认', '是否取消发布系统公告', function (r) {
                    if (r) {
                        $.post($.webPath() + '/base/xtgg/xtgg_unpub', {"xtgg.id": row.id}, function (data) {
                            var json = $.parseJSON(data);
                            if (json.statusCode == '200') {
                                xtgg_search();
                                $.messager.show({title: '提示框', msg: json.message, timeout: 3000, showType: 'slide'});
                            }
                            else
                                $.messager.alert('提示', json.message, 'info');
                        });
                    }
                });
            } else {
                $.messager.alert('提示', "请选择要取消的系统公告", 'info');
            }
        });
    }
}

//查询按钮
$("#btn_xtgg_search").click(function () {
    xtgg_search();
});
//查询系统公告
function xtgg_search() {
    $('#grid_xtgg').datagrid('load',
        {
            ggbt: $('#xtgg_ggbt').val(),
            ggnr: $('#xtgg_ggnr').val(),
            ggzt: $("#toolbar_xtgggl input[name='ggzt']").val(),
            xtggcjStart: $("#toolbar_xtgggl input[name='xtggcjStart']").val(),
            xtggcjEnd: $("#toolbar_xtgggl input[name='xtggcjEnd']").val()
        }
    );
}
$(function () {
    //表格初始加载
    xtgg.initDataGrid();
    xtgg.initBtn();
});